// David Andersen

using UnityEngine;
using System.Collections;

public class BSPGUI : MonoBehaviour {
	
	public Texture2D black;
	public Texture2D white;
	public Texture2D blue;
	public Texture2D red;
	float dungeonSize = 512;
	float splitRatio = 0.7F;
	float splitRange = 0.1F;
	public int nodeTotal = 1;
	public int roomTotal = 1;
	public BSPNode rootNode = new BSPNode(0,0,512,512,1);
	public Rect[] splits;
	public Rect[] rooms;
	public Rect[] corridors;
	bool splitDraw = false;
	bool roomDraw = false;
	bool corridorDraw = false;
			
	void OnGUI ()
	{
		GUILayout.BeginArea(new Rect(10,10,128,512));
		GUILayout.BeginVertical();
		
		GUILayout.Label("Dungeon Size: " + dungeonSize.ToString());
		dungeonSize = GUILayout.HorizontalSlider(dungeonSize,32,512);
		if(Mathf.IsPowerOfTwo(Mathf.RoundToInt(dungeonSize)) == false)
		{
			dungeonSize = Mathf.ClosestPowerOfTwo(Mathf.RoundToInt(dungeonSize));
		}
		
		GUILayout.Label("Split Ratio: " + splitRatio.ToString());
		splitRatio = (Mathf.RoundToInt(GUILayout.HorizontalSlider(splitRatio,0.1F,0.9F) * 10)) * 0.1F;
		
		GUILayout.Label("Split Range: " + splitRange.ToString());
		splitRange = (Mathf.RoundToInt(GUILayout.HorizontalSlider(splitRange,0.0F,0.9F) * 10)) * 0.1F;
		
		if(GUILayout.Button("Split"))
		{			
			rootNode.Split(splitRatio,splitRange);
			nodeTotal = rootNode.CountNodes(nodeTotal);
			roomTotal = rootNode.CountRooms(nodeTotal);
			splits = new Rect[nodeTotal];
			rootNode.TraverseSplit(splits);
			splitDraw = true;
			roomDraw = false;
			corridorDraw = false;
		}
		
		if(GUILayout.Button("Generate Rooms"))
		{
			rootNode.GenerateRoom();
			rooms = new Rect[roomTotal];
			rootNode.TraverseRooms(rooms,roomTotal);
			roomDraw = true;
			splitDraw = false;
			corridorDraw = false;
		}
		
		if(GUILayout.Button("Generate Corridors"))
		{
			rootNode.GenerateCorridors(rooms,roomTotal);
			corridors = new Rect[nodeTotal - roomTotal];
			rootNode.TraverseCorridors(corridors);
			corridorDraw = true;
			splitDraw = false;
		}
		
		if(GUILayout.Button("Generate Rooms\n and Corridors"))
		{
			rootNode.GenerateRoom();
			rooms = new Rect[roomTotal];
			rootNode.TraverseRooms(rooms,roomTotal);
			rootNode.GenerateCorridors(rooms,roomTotal);
			corridors = new Rect[nodeTotal - roomTotal];
			rootNode.TraverseCorridors(corridors);
			roomDraw = true;
			splitDraw = false;
			corridorDraw = true;
		}
		
		if(GUILayout.Button("Reset"))
		{
			Reset();
		}
		
		GUILayout.Label(nodeTotal.ToString());
		GUILayout.Label(roomTotal.ToString());
		GUILayout.Label(dungeonSize.ToString());
		GUILayout.Label(splitRatio.ToString());
		
		if(GUI.changed)
		{
			Reset();
		}
		
		GUILayout.EndVertical();
		GUILayout.EndArea();
		
		GUILayout.BeginArea(new Rect(200,10,dungeonSize,dungeonSize));
		
		if(Event.current.type.Equals(EventType.Repaint))
			{				
				Graphics.DrawTexture(new Rect(0,0,dungeonSize,dungeonSize), white);
			}
		
		if(splitDraw == true)
		{
			if(Event.current.type.Equals(EventType.Repaint))
			{				
				for(int i = 0; i < nodeTotal; i++)
				{
					Graphics.DrawTexture(splits[i], black);
				}
			}
		}
		
		if(roomDraw == true)
		{
			if(Event.current.type.Equals(EventType.Repaint))
			{				
				for(int i = 0; i < roomTotal; i++)
				{
					Graphics.DrawTexture(rooms[i], blue);
				}
			}
		}
		
		if(corridorDraw == true)
		{
			if(Event.current.type.Equals(EventType.Repaint))
			{				
				for(int i = 0; i < (nodeTotal - roomTotal); i++)
				{
					Graphics.DrawTexture(corridors[i], red);
				}
			}
		}
				
		GUILayout.EndArea();
				
	}
	
	void Reset()
	{
		rootNode = new BSPNode(0,0,dungeonSize,dungeonSize,1);
		splitDraw = false;
		roomDraw = false;
		corridorDraw = false;
		nodeTotal = 1;
		roomTotal = 1;
	}
}
